package com.ujoin.dao.imp;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.Activity;
import com.ujoin.dao.IActivityDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * Activity entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ujoin.bean.Activity
 * @author MyEclipse Persistence Tools
 */

public class ActivityDAO extends HibernateDaoSupport implements IActivityDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ActivityDAO.class);
	// property constants
	public static final String TITLE = "title";
	public static final String DATETIME_SCRIPT = "datetimeScript";
	public static final String CITY = "city";
	public static final String DISTRICT = "district";
	public static final String PLACE = "place";
	public static final String COST = "cost";
	public static final String FIRST_TYPE = "firstType";
	public static final String SECOND_TYPE = "secondType";
	public static final String INTEREST_NUM = "interestNum";
	public static final String PARTICIPANT_NUM = "participantNum";
	public static final String DETAIL = "detail";
	public static final String SUMMARY = "summary";
	public static final String IS_PRIVATE = "isPrivate";
	public static final String USER_NUM = "userNum";
	public static final String STATUS = "status";
	public static final String RELEASER_ID = "releaserId";
	public static final String UP_NUM = "upNum";
	public static final String TAG = "tag";
	public static final String VALID = "valid";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#save(com.ujoin.bean.Activity)
	 */
	@Override
	public void save(Activity transientInstance) {
		log.debug("saving Activity instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#delete(com.ujoin.bean.Activity)
	 */
	@Override
	public void delete(Activity persistentInstance) {
		log.debug("deleting Activity instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findById(java.lang.Integer)
	 */
	@Override
	public Activity findById(java.lang.Integer id) {
		log.debug("getting Activity instance with id: " + id);
		try {
			Activity instance = (Activity) getHibernateTemplate().get(
					"com.ujoin.bean.Activity", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByExample(com.ujoin.bean.Activity)
	 */
	@Override
	public List findByExample(Activity instance) {
		log.debug("finding Activity instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Activity instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Activity as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByTitle(java.lang.Object)
	 */
	@Override
	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByDatetimeScript(java.lang.Object)
	 */
	@Override
	public List findByDatetimeScript(Object datetimeScript) {
		return findByProperty(DATETIME_SCRIPT, datetimeScript);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByCity(java.lang.Object)
	 */
	@Override
	public List findByCity(Object city) {
		return findByProperty(CITY, city);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByDistrict(java.lang.Object)
	 */
	@Override
	public List findByDistrict(Object district) {
		return findByProperty(DISTRICT, district);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByPlace(java.lang.Object)
	 */
	@Override
	public List findByPlace(Object place) {
		return findByProperty(PLACE, place);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByCost(java.lang.Object)
	 */
	@Override
	public List findByCost(Object cost) {
		return findByProperty(COST, cost);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByFirstType(java.lang.Object)
	 */
	@Override
	public List findByFirstType(Object firstType) {
		return findByProperty(FIRST_TYPE, firstType);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findBySecondType(java.lang.Object)
	 */
	@Override
	public List findBySecondType(Object secondType) {
		return findByProperty(SECOND_TYPE, secondType);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByInterestNum(java.lang.Object)
	 */
	@Override
	public List findByInterestNum(Object interestNum) {
		return findByProperty(INTEREST_NUM, interestNum);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByParticipantNum(java.lang.Object)
	 */
	@Override
	public List findByParticipantNum(Object participantNum) {
		return findByProperty(PARTICIPANT_NUM, participantNum);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByDetail(java.lang.Object)
	 */
	@Override
	public List findByDetail(Object detail) {
		return findByProperty(DETAIL, detail);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findBySummary(java.lang.Object)
	 */
	@Override
	public List findBySummary(Object summary) {
		return findByProperty(SUMMARY, summary);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByIsPrivate(java.lang.Object)
	 */
	@Override
	public List findByIsPrivate(Object isPrivate) {
		return findByProperty(IS_PRIVATE, isPrivate);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByUserNum(java.lang.Object)
	 */
	@Override
	public List findByUserNum(Object userNum) {
		return findByProperty(USER_NUM, userNum);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByStatus(java.lang.Object)
	 */
	@Override
	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByReleaserId(java.lang.Object)
	 */
	@Override
	public List findByReleaserId(Object releaserId) {
		return findByProperty(RELEASER_ID, releaserId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByUpNum(java.lang.Object)
	 */
	@Override
	public List findByUpNum(Object upNum) {
		return findByProperty(UP_NUM, upNum);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByTag(java.lang.Object)
	 */
	@Override
	public List findByTag(Object tag) {
		return findByProperty(TAG, tag);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findByValid(java.lang.Object)
	 */
	@Override
	public List findByValid(Object valid) {
		return findByProperty(VALID, valid);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Activity instances");
		try {
			String queryString = "from Activity";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#merge(com.ujoin.bean.Activity)
	 */
	@Override
	public Activity merge(Activity detachedInstance) {
		log.debug("merging Activity instance");
		try {
			Activity result = (Activity) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#attachDirty(com.ujoin.bean.Activity)
	 */
	@Override
	public void attachDirty(Activity instance) {
		log.debug("attaching dirty Activity instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IActivityDAO#attachClean(com.ujoin.bean.Activity)
	 */
	@Override
	public void attachClean(Activity instance) {
		log.debug("attaching clean Activity instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IActivityDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IActivityDAO) ctx.getBean("ActivityDAO");
	}
}